/////////////////////////////////////////////////////////////////////////////
// 2023 -
//open source under Apache License Version 2.0
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once


#include <kmhdfs/client/byte_buffer.h>
#include <memory>

#include <vector>
#include <memory>
#include <map>
#include <algorithm>
#include <utility>
#include <mutex>

namespace Hdfs {
namespace Internal {

class GaloisField {
public:
    static std::shared_ptr<GaloisField> getInstance();
    static std::shared_ptr<GaloisField> getInstance(int fieldSize, int primitivePolynomial);

    int getFieldSize() const;

private:
    GaloisField() = default;
    explicit GaloisField(int _fieldSize);

    // Field size 256 is good for byte based system
    static const int DEFAULT_FIELD_SIZE = 256;
    // primitive polynomial 1 + X^2 + X^3 + X^4 + X^8 (substitute 2)
    static const int DEFAULT_PRIMITIVE_POLYNOMIAL = 285;
    static std::map<int, std::shared_ptr<GaloisField>> instances;
    static std::mutex singleton_mtx;

    int fieldSize;
};


}
}
